﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using Reporter.Data;
using ReporterControlLibrary.Models;

namespace Reporter.Controls
{
	/// <summary>
	/// Interaction logic for ImportProvisionNotebookDialogBox.xaml
	/// </summary>
	public partial class ImportProvisionNotebookDialogBox : Window
	{
        public ImportInfo ImportInfo { get; set; }

        public Project Project { get; set; }
        public bool ImportImages { get; set; }

        public ImportProvisionNotebookDialogBox()
		{
			this.InitializeComponent();
			
			// Insert code required on object creation below this point.
            Loaded += new RoutedEventHandler(ImportProvisionNotebookDialogBoxLoaded);

            // OKButton.IsEnabled = true;

		}

        void ImportProvisionNotebookDialogBoxLoaded(object sender, RoutedEventArgs e)
        {
            ImportInfo = LayoutRoot.DataContext as ImportInfo;
            if (ImportInfo != null)
            {
                ImportInfo.StartTimer();


                ThreadPool.QueueUserWorkItem(new WaitCallback(
                                                 (object o) =>
                                                     {
                                                         var importStatus = AppData.ImportProvisionNotebook(Project, ImportImages);

                                                         if (!importStatus)
                                                         {
                                                             OKButton.Dispatcher.Invoke(new Action(
                                                                                            () =>
                                                                                            {
                                                                                                this.Close();

                                                                                                ReporterControlLibrary.MessageBox.Show(null, "Provision is not installed or is corrupted", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                                                                                            }
                                                                                            ), null);

                                                             return;
                                                         }

                                                         //end of counting
                                                         OKButton.Dispatcher.Invoke(new Action(
                                                                                        () =>
                                                                                            {
                                                                                                //map real reportProject notebook into design project
                                                                                                Project.ProjectInfo.NotebookName = Project.ReportProject.Notebook.NotebookName;
                                                                                                Project.ProjectInfo.ImportedDate = Project.ReportProject.Notebook.LastImportDate;
                                                                                                Project.ProjectInfo.ProvisionFileVersion = Project.ReportProject.Notebook.ProvisionVersion;

                                                                                                ImportInfo.StopTimer();
                                                                                                
                                                                                                this.importProgressText.Visibility = Visibility.Collapsed;
                                                                                                this.importFinishedText.Visibility = Visibility.Visible;
                                                                                                importImages_chb.IsChecked = true;
                                                                                                OKButton.IsEnabled = true;
                                                                                            }
                                                                                        ), null);
                                                     }
                                                 ));
            }
        }

	    private void OkBtClick(object sender, RoutedEventArgs e)
        {
            // form validation comes here

            this.DialogResult = true;
            ImportInfo.StopTimer();
        }
	}
}